package core.view
{
	import core.model.ConfigData;
	
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.getTimer;
	
	import org.ais.event.TEvent;
	import org.ais.system.Ais;
	import org.aisy.display.USprite;
	import org.aisy.listoy.Listoy;
	import org.aisy.skin.AisySkin;

	public class ConfigEditView extends USprite
	{
		static protected var instance:ConfigEditView;
		protected var NAME:String;
		protected var _textField:TextField;
		
		public function ConfigEditView()
		{
			init();
		}
		
		protected function init():void
		{
			NAME = "" + getTimer();
			
			_textField = new TextField();
			_textField.defaultTextFormat = AisySkin.TEXTFORMAT();
			_textField.type = "input";
			_textField.wordWrap = true;
			_textField.multiline = true;
			_textField.background = true;
			_textField.alpha = 0.7;
			_textField.width = Ais.IMain.stage.stageWidth * 0.7;
			_textField.height = Ais.IMain.stage.stageHeight * 0.7;
			
			if (null !== ConfigData.configData) _textField.appendText(JSON.stringify(ConfigData.configData));
			
			addChild(_textField);
			
			var arr:Array = [
				{"op": "save", "text": "Save"}
			];
			
			var listoy:Listoy = new Listoy();
			listoy.setRowColumn(1, arr.length);
			listoy.setItemRenderer(OpItem);
			listoy.setDataProvider(arr);
			listoy.initializeView();
			listoy.y = this.height + 7;
			addChild(listoy);
			
			TEvent.newTrigger(listoy.NAME, __triggerHandler);
			
			TEvent.trigger("UP_WINDOW_AIS", "SHOW", [this, NAME, 2, true, true, 0x333333]);
		}
		
		protected function __triggerHandler(type:String, data:*):void
		{
			switch (type) {
				case "OP":
					switch (data.op) {
						case "save":
							var obj:*;
							try {
								obj = JSON.parse(_textField.text);
							}
							catch (error:Error) {
								return;
							}
							ConfigData.configData = obj;
							ConfigData.writeConfigData();
							CopyFileView.getInstance().initGroup();
							TEvent.trigger("UP_WINDOW_NEW", "CLEAR", {"name": NAME});
							break;
					}
					break;
			}
		}
		
		override public function clear():void
		{
			_textField = null;
			NAME = null;
			instance = null;
			super.clear();
		}
		
		static public function getInstance():ConfigEditView
		{
			if (null === instance) instance = new ConfigEditView();
			return instance;
		}
		
	}
}
import flash.events.MouseEvent;
import flash.text.TextField;

import org.ais.event.TEvent;
import org.aisy.component.TextView;
import org.aisy.listoy.ListoyItem;
import org.aisy.skin.AisySkin;

internal class OpItem extends ListoyItem
{
	public function OpItem(name:String, index:uint, data:*):void
	{
		super(name, index, data);
		init();
	}
	
	protected function init():void
	{
		var tv:TextView = new TextView();
		tv.setText(itemInfo.text);
		tv.setColor(0xCCCCCC);
		tv.dynamic = {"mouseEnabled": false};
		tv.buttonMode = true;
		tv.addEventListener(MouseEvent.CLICK, __mouseHandler);
		addChild(tv);
	}
	
	protected function __mouseHandler(e:MouseEvent):void
	{
		switch (e.type) {
			case MouseEvent.CLICK:
				TEvent.trigger(NAME, "OP", itemInfo);
				break;
		}
	}
	
}